package cn.youyeku.zsxh.ui.activity.ABS;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.LogWriter;

import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.io.IOException;

import cn.youyeku.zsxh.Utils.FileUtils;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.app;

import static android.R.attr.breadCrumbShortTitle;
import static android.R.attr.maxHeight;
import static android.R.attr.maxWidth;

/**
 * Created by youye on 17-4-4.
 */

public abstract class FeedbackActivity extends BaseActivity {
    private static final int REQUEST_PERMISSION = 999;
    protected final int CAMERA_REQUEST_CODE = 1;//相机
    protected final int GALLERY_REQUEST_CODE = 2;//本地


    protected String IMAGE_FILE_NAME = "SampleCropImage.jpg";

    protected String local_file_path = "";


    protected abstract void setPicturePreview(Uri resultUri);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.w("返回结果类型" + requestCode);
        switch (requestCode) {
            case GALLERY_REQUEST_CODE:
                try {
                    startImageZoom(data.getData());
                } catch (Exception e) {
                    e.printStackTrace();// 用户点击取消操作
                }
                break;
            case CAMERA_REQUEST_CODE:

                try {
                    File temp = new File(getExternalCacheDir(), IMAGE_FILE_NAME);
                    LogUtils.w(temp.toString());
                    startImageZoom(Uri.fromFile(temp));
                } catch (Exception e) {
                    app.backgroundThreadShortToast("没有SD卡？？");
                    e.printStackTrace();
                }
                break;
            case UCrop.REQUEST_CROP:
                LogUtils.w("REQUEST_CROP");
                if (resultCode == RESULT_OK) {
                    final Uri resultUri = UCrop.getOutput(data);
                    local_file_path = resultUri.getPath();
                    setPicturePreview(resultUri);
                }

                break;
            default:
                LogUtils.w("不再范围内");


        }

    }


    public void startImageZoom(Uri sourceUri) {
        Uri destinationUri = Uri.fromFile(new File(getExternalCacheDir(), "SampleCropImage.jpg"));

        UCrop.of(sourceUri, destinationUri)
                .withAspectRatio(16, 9)
                .withMaxResultSize(maxWidth, maxHeight)
                .start(this);
    }

}
